package com.example.ljy.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDao {

    DBHelper dbOpenHelper;
    private static UserDao instance;

    UserDao(Context context){
        dbOpenHelper = new DBHelper(context,"ccc");
    }

    public static UserDao getInstance(Context context) {
        if (instance == null) {
            instance = new UserDao(context);
        }
        return instance;
    }

    public boolean insert(UserBean bean){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("time",bean.getTime());
        values.put("temperature",bean.getTemperature());
        /*values.put("age",bean.getAge());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        values.put("registerdate",dateFormat.format(bean.getRegisterdate()));*/
        long i = db.insert(DBHelper.TABLE_NAME,null,values);
        db.close();
        return (i>0);
    }

    public int delete(long id) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int delete = db.delete(DBHelper.TABLE_NAME, "_id=?", new String[]{String.valueOf(id)});
        db.close();
        return delete;
    }

    public int deleteAll() {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int delete = db.delete(DBHelper.TABLE_NAME, null, null);
        db.close();
        return delete;
    }

    public boolean checkPassword(String name, String password){
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor c = db.query(DBHelper.TABLE_NAME, null, "name=? and password=?",
                new String[]{name,password},null,null,null);
        boolean b = c.getCount()>0;
        db.close();
        return b;
    }

    @SuppressLint("Range")
    public List<Map<String,?>> getAllUsers() {
        List<Map<String,?>> list = new ArrayList<>();
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor c = db.query(DBHelper.TABLE_NAME, null, null,
                null,null,null,null);
        while (c.moveToNext()) {
            Map map = new HashMap();
            map.put("time", c.getString(c.getColumnIndex("time")));
            map.put("temperature", c.getString(c.getColumnIndex("temperature")));
           // map.put("registerdate", c.getString(c.getColumnIndex("registerdate")));
            list.add(map);
        }
        db.close();
        return list;
    }

    public boolean modifyUserInfo(UserBean bean) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        /*values.put("password",bean.getPassword());
        values.put("age",bean.getAge());*/
        values.put("temperature",bean.getTemperature());
        long i = db.update(DBHelper.TABLE_NAME,values,
                "time=?",new String[]{bean.getTime()});
        db.close();
        if(i>0)return true;
        else return false;
    }

    public void creatDAtabase(){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.close();

    }
    public Map<String, ?> getUserById(int id) {
        return null;
    }

}
